package com.boot2523.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    /**
     * 在spring容器中注册一个RedisTemplate的bean，可进行redis的常用操作
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> template=new RedisTemplate<>();
        template.setConnectionFactory(factory);
        //保证key是可读的字符串
        template.setKeySerializer(new StringRedisSerializer());
        //支持直接存取java对象,自动转为json
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}
